package test.my.com.myapplicationtest.yuan_hu_arc;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import test.my.com.myapplicationtest.R;

/**
 * Author : Cooper
 * Time : 2017/1/17  14:10
 * Description : 动态圆弧view
 */
public class YuanHuArcView extends View {

    private Context mContext;

    private int mWidth = 100;
    private int mHeight = 100;

    private int mArcColor; // 圆弧颜色
    private int mArcStrokeWidth; // 圆弧宽度
    private int mStartAngle; // 圆弧起始角度
    private int mAnimatorTime; // 圆弧动画时间
    private RectF mRectF;
    private int mSpace = 4; // 圆弧偏移（解决边缘显示不全）

    private Paint mPaint;
    private int mSweepAngle; // 圆弧画的角度
    private ValueAnimator valueAnimator;

    public YuanHuArcView(Context context) {
        super(context);
    }

    public YuanHuArcView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public YuanHuArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.YuanHuArcView,defStyleAttr,0);
        mArcColor = ta.getColor(R.styleable.YuanHuArcView_arcColor, Color.RED);
        mArcStrokeWidth = ta.getDimensionPixelOffset(R.styleable.YuanHuArcView_arcStrokeWidth,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2,
                getResources().getDisplayMetrics()));
        mStartAngle = ta.getInt(R.styleable.YuanHuArcView_startAngle,0);
        mSweepAngle = ta.getInt(R.styleable.YuanHuArcView_sweepAngle,100);
        mAnimatorTime = ta.getInt(R.styleable.YuanHuArcView_animatorTime,1000);
        ta.recycle();

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(mArcColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mArcStrokeWidth);

        valueAnimator = ValueAnimator.ofInt(0,mSweepAngle);
        valueAnimator.setDuration(mAnimatorTime);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
//        valueAnimator.setRepeatCount(10);
//        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mSweepAngle = (int) animation.getAnimatedValue();
                YuanHuArcView.this.invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
        mRectF = new RectF(mSpace,mSpace,mWidth-mSpace,mHeight-mSpace);
    }

    private int measureWidth(int widthMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mWidth;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mWidth = result;
        return result;
    }

    private int measureHeight(int heightMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mHeight;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mHeight = result;
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * oval：圆弧所在的椭圆对象。
         startAngle：圆弧的起始角度。
         sweepAngle：圆弧的角度。
         useCenter：是否显示半径连线，true表示显示圆弧与圆心的半径连线，false表示不显示。
         paint：绘制时所使用的画笔。
         */
        canvas.drawArc(mRectF,mStartAngle,mSweepAngle,false,mPaint);
    }

    /**
     * 开始动画
     */
    public void startAnimatorArc() {
        valueAnimator.start();
    }

}
